<?php
require_once 'common/config.php';
require_once 'common/functions.php';
// 检查登录状态和权限
check_login();
if ($_SESSION['role'] != 1) {
    header("Location: index.php");
    exit();
}
// 获取管理员列表
$db = db_connect();
$search = isset($_GET['search']) ? $_GET['search'] : '';
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
// 构建查询条件
$where = "1=1";
if ($search) {
    $where .= " AND (username LIKE :search OR name LIKE :search OR phone LIKE :search)";
}
// 获取总记录数
$countStmt = $db->prepare("SELECT COUNT(*) FROM admin_users WHERE {$where}");
if ($search) {
    $countStmt->bindValue(':search', "%{$search}%");
}
$countStmt->execute();
$total = $countStmt->fetchColumn();
$total_pages = ceil($total / $limit);
// 获取当前页数据
$sql = "SELECT * FROM admin_users WHERE {$where} ORDER BY id DESC LIMIT :offset, :limit";
$stmt = $db->prepare($sql);
if ($search) {
    $stmt->bindValue(':search', "%{$search}%");
}
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
$admins = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
</head>
<body>
    <div class="wrapper">
        <!-- 引入侧边栏 -->
        <?php include 'includes/sidebar.php'; ?>

        <!-- 主要内容区 -->
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>管理员管理</h2>
                </div>
                <div class="header-right">
                    <a href="admin_user_add.php" class="btn btn-primary"><i class="fas fa-plus"></i> 添加管理员</a>
                </div>
            </header>

            <div class="content">
                <!-- 搜索栏 -->
                <div class="search-bar">
                    <form action="" method="GET">
                        <input type="text" name="search" placeholder="搜索管理员..." value="<?php echo htmlspecialchars($search); ?>">
                        <button type="submit"><i class="fas fa-search"></i></button>
                    </form>
                </div>

                <!-- 数据表格 -->
                <div class="data-table">
                    <table>
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>头像</th>
                                <th>用户名</th>
                                <th>姓名</th>
                                <th>角色</th>
                                <th>状态</th>
                                <th>手机号</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($admins as $admin): ?>
                            <tr>
                                <td><?php echo $admin['id']; ?></td>
                                <td>
                                    <?php if ($admin['avatar']): ?>
                                    <img src="../api/user/<?php echo htmlspecialchars($admin['avatar']); ?>" class="avatar-small" alt="头像">
                                    <?php else: ?>
                                    <i class="fas fa-user-circle"></i>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo htmlspecialchars($admin['username']); ?></td>
                                <td><?php echo htmlspecialchars($admin['name']); ?></td>
                                <td>
                                    <span class="badge <?php echo $admin['role'] == 1 ? 'badge-primary' : 'badge-secondary'; ?>">
                                        <?php echo $admin['role'] == 1 ? '超级管理员' : '普通管理员'; ?>
                                    </span>
                                </td>
                                <td>
                                    <span class="badge <?php echo $admin['status'] == 1 ? 'badge-success' : 'badge-danger'; ?>">
                                        <?php echo $admin['status'] == 1 ? '正常' : '禁用'; ?>
                                    </span>
                                </td>
                                <td><?php echo htmlspecialchars($admin['phone']); ?></td>
                                <td>
                                    <a href="admin_user_edit.php?id=<?php echo $admin['id']; ?>" class="btn-icon" title="编辑">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="javascript:void(0)" onclick="deleteAdmin(<?php echo $admin['id']; ?>)" class="btn-icon text-danger" title="删除">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                <?php if ($total_pages > 1): ?>
                <div class="pagination">
                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                        <a href="?page=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>" 
                           class="<?php echo $page == $i ? 'active' : ''; ?>">
                            <?php echo $i; ?>
                        </a>
                    <?php endfor; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script>
    function deleteAdmin(id) {
        if (confirm('确定要删除这个管理员吗？')) {
            // 创建表单数据
            const formData = new FormData();
            formData.append('id', id);

            // 发送POST请求
            fetch('admin_user_delete.php', {
                method: 'POST',
                body: formData,
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // 显示成功提示并刷新页面
                    alert(data.message);
                    window.location.reload();
                } else {
                    alert('删除失败：' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('删除成功');
            });
        }
    }
    </script>
</body>
</html> 